﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class sweetmove : MonoBehaviour
{
    private Gamesweet gamesweet;

    private IEnumerator moveCon;//得到其他指令时终止次协程
    private void Awake()
    {
        gamesweet = GetComponent<Gamesweet>();
    }
    //控制moveCon协程的开启关闭
    public void Move(int newx, int newy, float time)
    {
        if (moveCon != null)
        {
            StopCoroutine(moveCon);
        }
        moveCon = MoveCor(newx, newy, time);
        StartCoroutine(moveCon);
 }
    private IEnumerator MoveCor(int newx, int newy,float time)//移动的协程
    {
        gamesweet.X = newx;
        gamesweet.Y = newy;
        Vector3 startpos = transform.position;
        Vector3 endpos = gamesweet.gamemanager.reset_pos(newx, newy);
        for (float index = 0; index < time; index += Time.deltaTime)
        {
            gamesweet.transform.position = Vector3.Lerp(startpos, endpos, index / time);
            yield return 0;
        }
        gamesweet.transform.position = endpos;
    }
}
